import os
import sys
project_root = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if project_root not in sys.path:
    sys.path.append(project_root)

from readlater.config.config_manager import ConfigManager
from readlater.content_downloader.webpage_visitor import WebpageVisitor
from readlater.utils.logger import get_logger



# 启动服务
if __name__ == "__main__":
    logger = get_logger(__name__)
    logger.info("启动服务")

    # 初始化数据库
    config_manager = ConfigManager()
    webpage_visitor = WebpageVisitor()
    articles = webpage_visitor.search_download_list()
    for article in articles:
        logger.info("-"*10)
        logger.info(f"文章标题: {article.get('title')}")
        logger.info(f"文章链接: {article.get('url')}")
        logger.info(f"文章id: {article.get('id')}")
        logger.info(f"当前状态: {article.get('status')}")
        logger.info(f"创建时间: {article.get('datetime')}")
        logger.info("="*10)

    logger.info(f"共获取到 {len(articles)} 篇文章")

    success_list, failed_list = webpage_visitor.download_webpages(articles)
    logger.info(f"成功下载 {len(success_list)} 篇文章")
    logger.info(f"失败下载 {len(failed_list)} 篇文章")
    if failed_list:
        logger.info("失败文章列表:")
        for article in failed_list:
            logger.info(f"{article.get('title')}")
    logger.info("服务结束")





